// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DoubleSubscriptionAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Runtime
{
    using System.Runtime.Serialization;
    using System.Threading;
    using Microsoft.Robotics.Runtime;
    
    /// <summary>
    /// An agent that implements the ISubscriptionReceiver twice.
    /// Used to validate that the metadata discovery code correctly find and disambiguates between the two.
    /// </summary>
    [DataContract]
    public class DoubleSubscriptionAgent : Agent, 
                                           ISubscriptionReceiver<SimpleAgentMessage<int>>, 
                                           ISubscriptionReceiver<SimpleAgentMessage<double>>
    {
        /// <summary>
        /// True if the first Receive was called.
        /// </summary>
        private bool wasReceive1Called;

        /// <summary>
        /// True if the second Receive was called.
        /// </summary>
        private bool wasReceive2Called;

        /// <summary>
        /// The agent we are subscribing to.
        /// </summary>
        private IAgentHost producer;

        /// <summary>
        /// Initializes a new instance of the DoubleSubscriptionAgent class.
        /// </summary>
        /// <param name="name">The name of the agent instance.</param>
        public DoubleSubscriptionAgent(string name)
            : base(name)
        {
            this.AllMethodsCalledEvent = new ManualResetEvent(false);
        }

        /// <summary>
        /// Gets an event that signals when both Receive methods have been called.
        /// </summary>
        /// <returns>True if both Receive methods have been called.</returns>
        public ManualResetEvent AllMethodsCalledEvent
        {
            get;
            private set;
        }

        /// <summary>
        /// Finds and subscribes to the publishing agent.
        /// </summary>
        /// <param name="locator">A locator to use.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            this.producer = locator.GetAgent("EchoAgent");
            this.producer.Subscribe<SimpleAgentMessage<int>>(this);
            this.producer.Subscribe<SimpleAgentMessage<double>>(this);
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(SimpleAgentMessage<int> message)
        {
            this.wasReceive1Called = true;
            if (this.wasReceive2Called)
            {
                this.AllMethodsCalledEvent.Set();
            }
        }

        /// <summary>
        /// Called to process subscription messages of a given type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        public void Receive(SimpleAgentMessage<double> message)
        {
            this.wasReceive2Called = true;
            if (this.wasReceive1Called)
            {
                this.AllMethodsCalledEvent.Set();
            }
        }

        /// <summary>
        /// Activates upstream agents.
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();
            this.producer.Activate();
        }

        /// <summary>
        /// Activates upstream agents.
        /// </summary>
        public override void OnDeactivated()
        {
            base.OnDeactivated();
            this.producer.Deactivate();
        }
    }
}
